//
//  CZWelcomeViewController.swift
//  iOSWeibo10
//
//  Created by 李伟鹏 on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class CZWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        setIcon()
        CZUserAccountViewModel.sharedInstance.loadUserInfo { () -> () in
            self.setIcon()
        }
    }
    
    private func setIcon(){
        if let avatar_large = CZUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
            let url = NSURL(string: avatar_large)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }

    private func moveAnimation(){
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
                        appDelegate.switchRootViewController(CZMainViewController())
                })
        }
    }
    
    private func prepareUI(){
        view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        bkgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    private lazy var bkgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(15)
        label.sizeToFit()
        label.alpha = 0
        return label
    }()
}
